package cn.tedu.yl.Health_check_up_package.service.Impl;

import cn.tedu.yl.TCLX.mapper.nxtcMapper;
import cn.tedu.yl.TCLX.pojo.nxtc;
import cn.tedu.yl.base.response.PageData;
import cn.tedu.yl.Health_check_up_package.mapper.physical_examination_packageMapper;
import cn.tedu.yl.Health_check_up_package.pojo.physical_examination_package;
import cn.tedu.yl.Health_check_up_package.service.physical_examination_packageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.SecureRandom;
import java.util.Random;

@Service
@Slf4j
public class physical_examination_packageServiceImpl implements physical_examination_packageService {

    @Autowired
    private physical_examination_packageMapper articleRepository;

    @Autowired
    private nxtcMapper  nxtcMapper;
    @Override
    public PageData getAllDepartment() {
        PageData pageData = new PageData();
        pageData.setList(articleRepository.findAll());
        pageData.setTotal(articleRepository.findAll().size());
        return pageData;
    }

    @Override
    public void createArticle(physical_examination_package physicalExaminationPackage) {
//        设置套餐id
        physicalExaminationPackage.setPackageId(setpackageId());
//       判断
        if(physicalExaminationPackage.getPackageType() == 11){
            log.info("插入男性套餐");
            nxtc n = new nxtc();
            n.setContainName(physicalExaminationPackage.getPackageName());
            n.setTclxId(physicalExaminationPackage.getPackageType());
            nxtcMapper.save(n);
        }else if(physicalExaminationPackage.getPackageType() == 12){
            log.info("插入女性套餐");
            nxtc n = new nxtc();
            n.setContainName(physicalExaminationPackage.getPackageName());
            n.setTclxId(physicalExaminationPackage.getPackageType());
            nxtcMapper.save(n);
        }else if(physicalExaminationPackage.getPackageType() == 13){
            log.info("插入老年人套餐");
            nxtc n = new nxtc();
            n.setContainName(physicalExaminationPackage.getPackageName());
            n.setTclxId(physicalExaminationPackage.getPackageType());
            nxtcMapper.save(n);
        }else if(physicalExaminationPackage.getPackageType() == 14){
            log.info("插入儿童套餐");
            nxtc n = new nxtc();
            n.setContainName(physicalExaminationPackage.getPackageName());
            n.setTclxId(physicalExaminationPackage.getPackageType());
            nxtcMapper.save(n);
        }

        articleRepository.save(physicalExaminationPackage);
    }

    private String setpackageId() {
        SecureRandom secureRandom = new SecureRandom();
        String packageId = "GWP" + secureRandom.nextInt(1000000000);
        return packageId;
    }

    @Override
    public void updateArticle(Long id, physical_examination_package physicalExaminationPackage) {
        physical_examination_package physicalExaminationPackage1 = articleRepository.findById(id).orElse(null);
        if (physicalExaminationPackage1 != null){
            physicalExaminationPackage1.setPackageId(physicalExaminationPackage.getPackageId());
        physicalExaminationPackage1.setPackageName(physicalExaminationPackage.getPackageName());
        physicalExaminationPackage1.setPackageImage(physicalExaminationPackage.getPackageImage());
        physicalExaminationPackage1.setPackageType(physicalExaminationPackage.getPackageType());
        physicalExaminationPackage1.setCampus(physicalExaminationPackage.getCampus());
        physicalExaminationPackage1.setPackagePrice(physicalExaminationPackage.getPackagePrice());
        physicalExaminationPackage1.setPackageProject(physicalExaminationPackage.getPackageProject());
        physicalExaminationPackage1.setPackageStatus(physicalExaminationPackage.getPackageStatus());
        articleRepository.save(physicalExaminationPackage1);
        }

    }

    @Override
    public void deleteDepartment(Long id) {
        articleRepository.deleteById(id);
    }
}
